package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.dto.e.DataAnalysisTalkEntDTO;
import com.macro.mall.dto.o.DataAnalysisTalkOutDTO;
import com.macro.mall.mapper.BasicTopicMapper;
import com.macro.mall.mapper.DpsTagTopicMapper;
import com.macro.mall.model.BasicTopic;
import com.macro.mall.model.DpsTagTopic;
import com.macro.mall.service.DataAnalysisTalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DataAnalysisTalkServiceImpl implements DataAnalysisTalkService {
    @Autowired
    private DpsTagTopicMapper dpsTagTopicMapper;
    @Autowired
    private BasicTopicMapper basicTopicMapper;
    @Override
    public PageInfo<DataAnalysisTalkOutDTO> list(DataAnalysisTalkEntDTO dataAnalysisTalkEntDTO) {
        PageHelper.startPage(dataAnalysisTalkEntDTO.getPage(),dataAnalysisTalkEntDTO.getPageSize());
        List<DpsTagTopic> list = dpsTagTopicMapper.list(dataAnalysisTalkEntDTO.getLabel());
        PageInfo<DpsTagTopic> dpsTagTopicPageInfo = new PageInfo<>(list);
        List<DataAnalysisTalkOutDTO> list1 =new ArrayList<>();
        for (DpsTagTopic dpsTagTopic:list){
            DataAnalysisTalkOutDTO dataAnalysisTalkOutDTO = new DataAnalysisTalkOutDTO();
            dataAnalysisTalkOutDTO.setId(dpsTagTopic.getId());
            dataAnalysisTalkOutDTO.setCreatedDt(dpsTagTopic.getCreatedDt());
            dataAnalysisTalkOutDTO.setLabel(dpsTagTopic.getKeyType());
            dataAnalysisTalkOutDTO.setStatus(dpsTagTopic.getStatus());
            dataAnalysisTalkOutDTO.setTalkName(dpsTagTopic.getTopicName());
            list1.add(dataAnalysisTalkOutDTO);
        }
        PageInfo<DataAnalysisTalkOutDTO> dataAnalysisTalkOutDTOPageInfo = new PageInfo<>(list1);
        dataAnalysisTalkOutDTOPageInfo.setTotal(dpsTagTopicPageInfo.getTotal());
        return dataAnalysisTalkOutDTOPageInfo;
    }

    @Override
    public int modify(DataAnalysisTalkEntDTO dataAnalysisTalkEntDTO) {
        if (dataAnalysisTalkEntDTO.getStatus()!=null&&dataAnalysisTalkEntDTO.getStatus()!=""){
            DpsTagTopic dpsTagTopic = new DpsTagTopic();
            dpsTagTopic.setId(dataAnalysisTalkEntDTO.getId());
            dpsTagTopic.setStatus(dataAnalysisTalkEntDTO.getStatus());
            return dpsTagTopicMapper.updateByPrimaryKeySelective(dpsTagTopic);
        }else {
            DpsTagTopic dpsTagTopic1 = dpsTagTopicMapper.selectByPrimaryKey(dataAnalysisTalkEntDTO.getId());
            BasicTopic basicTopic = new BasicTopic();
            basicTopic.setId(String.valueOf(dpsTagTopic1.getBasicTopicId()));
            basicTopic.setTopicName(dataAnalysisTalkEntDTO.getBasicTalk());
            basicTopicMapper.updateByPrimaryKeySelective(basicTopic);

            DpsTagTopic dpsTagTopic = new DpsTagTopic();
            dpsTagTopic.setId(dataAnalysisTalkEntDTO.getId());
            dpsTagTopic.setStatus(dataAnalysisTalkEntDTO.getStatus());
            dpsTagTopic.setKeyType(dataAnalysisTalkEntDTO.getLabel());
            return dpsTagTopicMapper.updateByPrimaryKeySelective(dpsTagTopic);
        }
    }
}
